package com.chenwc.file;

import com.chenwc.util.Utils;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 * 解压工具
 *
 * @author mrwang
 */
public class Decompression {

    protected static Logger log = LoggerFactory.getLogger(Decompression.class);

    /**
     * 获取car文件的appName和appVersion
     * @param carFile car文件
     * @return car文件的appName和appVersion
     */
    public static Map<String, String> getUploadCarbonAppName(File carFile){
        Map<String, String> carMap = new HashMap<>();
        String artifacts_xml = uncompress(carFile);
        if (StringUtils.isEmpty(artifacts_xml)){
            log.info(carFile.getName() + " 文件没有 artifacts.xml 文件！");
            return carMap;
        }
        Document document;
        try {
            SAXReader saxReader = new SAXReader();
            document = saxReader.read(new ByteArrayInputStream(artifacts_xml.getBytes(StandardCharsets.UTF_8)));
            Element root = document.getRootElement();
            carMap.put("appName", root.element("artifact").attribute("name").getValue());
            carMap.put("appVersion", root.element("artifact").attribute("version").getValue());
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return carMap;
    }

    /**
     * 压缩文件解压
     *
     * @param carFile 需要解压的jar文件
     * @return artifacts.xml 文件内容
     */
    public static String uncompress(File carFile) {
        JarFile jfInst = null;
        InputStream ins = null;
        String artifacts_xml = null;
        try {
            jfInst = new JarFile(carFile);
            Enumeration<JarEntry> enumEntry = jfInst.entries();
            while (enumEntry.hasMoreElements()) {
                JarEntry jarEntry = enumEntry.nextElement();
                if ("artifacts.xml".equals(jarEntry.getName())) {
                    //把jar实体转换为输入流
                    ins = jfInst.getInputStream(jarEntry);
                    byte[] artifacts = Utils.readInputStream(ins);
                    artifacts_xml = new String(artifacts, StandardCharsets.UTF_8);
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != ins) {
                    ins.close();
                }
                if (null != jfInst) {
                    jfInst.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return artifacts_xml;
    }

    /**
     * 测试文件解压
     *
     * @param args 传参
     */
    public static void main(String[] args) {

        long start = System.currentTimeMillis();
        //需要解压的文件
        File jarFile = new File(
                "E:\\IdeaProjects_Workspace\\wso2-upload-car-file\\saveFile\\TestCompositeApplication_1.0.0.car");
        log.info(Decompression.getUploadCarbonAppName(jarFile).toString());
        long end = System.currentTimeMillis();
        log.info("总耗时：" + (end - start) / 1000.00 + " 秒");
    }
}
